﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GRC_TPE_WebApp.Model;

namespace GRC_TPE_WebApp.Controller
{
    public static class CommunicationController
    {
        /// <summary>
        /// Méthode permettant la modification d'une communication en base
        /// </summary>
        /// <param name="idCommunication"></param>
        /// <param name="date"></param>
        /// <param name="description"></param>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string EditCommunication(int idCommunication, DateTime date, String description, String title, String type)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                var currentCommunication = (from communication in database.CommunicationSet
                                            where communication.Id == idCommunication
                                            select communication).First();

                ((Communication)currentCommunication).Date = date;
                ((Communication)currentCommunication).Description = description;
                ((Communication)currentCommunication).Title = title;
                ((Communication)currentCommunication).Type = type;


                database.SaveChanges();

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return "Erreur lors de la mise à jour de la communication";
            }
        }
    
        /// <summary>
        /// Permet d'ajouter une communication en base
        /// </summary>
        /// <param name="date"></param>
        /// <param name="description"></param>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <param name="clientId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string AddCommunication(DateTime date, String description, String title, String type, String clientId, String userId)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                //Création d'une communication temporaire, afin d'effectuer l'insertion en base
                Communication currentCommunication = new Communication();

                //Création d'un user temporaire, afin d'effectuer l'insertion en base
                //User currentUser = new User();

                //Création d'un client temporaire, afin d'effectuer l'insertion en base
                //Client currentClient = new Client();

                //Transforme la string clientID en paramètre en int, sinon déclenche l'erreur "Erreur lors de l'ajout d'une Communication"
                int RequestclientID = int.Parse(clientId);

                //Transforme la string userID en paramètre en int, sinon déclenche l'erreur "Erreur lors de l'ajout d'une Communication"
                int RequestuserID = int.Parse(userId);

                //Recherche du client correspond à l'id en paramètre
                //try
                //{
                //    currentClient = (from client in database.ClientSet
                //                     where client.Id == RequestclientID
                //                     select client).First();
                //}
                //catch (Exception e)
                //{
                //    return "Aucun client trouvé";
                //}

                //Recherche du client correspond à l'id en paramètre
                //try
                //{
                //    currentUser = (from user in database.UserSet
                //                     where user.Id == RequestuserID
                //                     select user).First();
                //}
                //catch (Exception e)
                //{
                //    return "Aucun utilisateur trouvé";
                //}

                //Ajout des paramètres a la communication courante
                currentCommunication.Date = date;
                currentCommunication.Description = description;
                currentCommunication.Title = title;
                currentCommunication.Type = type;
                currentCommunication.ClientId = RequestclientID;
                currentCommunication.UserId = RequestuserID;
   
                //Ajout en base
                database.AddToCommunicationSet(currentCommunication);

                //Commit
                database.SaveChanges();

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return "Erreur lors de l'ajout d'une communication";
            }
        }

        /// <summary>
        /// Méthode retournant les champs de la communication sous forme d'un tableau
        /// </summary>
        /// <param name="idCommunication"></param>
        /// <returns></returns>
        public static string[] GetCommunicationFields(int idCommunication)
        {
            try
            {
                string[] ret = null;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération de l'utilisateur par son id
                Communication existingCommunication = (from com in database.CommunicationSet.Include("User").Include("Client")
                                     where com.Id == idCommunication
                                     select com).First();

                if (existingCommunication != null)
                {
                    ret = new string[6];

                    ret[0] = ""+existingCommunication.Date;
                    ret[1] = existingCommunication.Description;
                    ret[2] = existingCommunication.Title;
                    ret[3] = existingCommunication.Type;
                    ret[4] = existingCommunication.Client.Name;
                    ret[5] = existingCommunication.User.Firstname + " " + existingCommunication.User.Lastname;
                   
                }
                else
                {
                    ret = new string[1];
                    ret[0] = String.Format("La communication d'id '{0}' n'existe pas.", idCommunication);
                }

                return ret;
            }
            catch (Exception ex)
            {
                string[] ret = new string[1];

                ret[0] = String.Format("Une erreur est survenue lors de la récupération de la communication d'id '{0}' en base.", idCommunication);

                return ret;
            }
        }

        /// <summary>
        /// Suppression d'une communication
        /// </summary>
        /// <param name="idCommunication"></param>
        /// <returns></returns>
        public static string DeleteCommunication(int idCommunication)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération de l'affaire par son id
                Communication existingCommunication = (from com in database.CommunicationSet
                                                       where com.Id == idCommunication
                                                       select com).First();

                if (existingCommunication != null)
                {
                    database.DeleteObject(existingCommunication);
                    database.SaveChanges();
                }
                else
                {
                    ret = String.Format("La communication d'id '{0}' n'existe pas.", idCommunication);
                }

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return string.Format("Erreur lors de la suppression de la communication d'id '{0}'.", idCommunication);
            }
        }
    }
}